//页面加载完成
$(function () {


    var  accident_datagrid =  $("#accident_datagrid");

    var accident_form =  $("#accident_form");

    var accident_dialog =  $("#accident_dialog");

    var searchBox=$("#searchBox");

    searchBox.searchbox({
        searcher:function(value,name){
            accident_datagrid.datagrid("load",{keyword:value});
        },
        prompt:'关键字'
    });
    //保单下拉框
    $("#policySn").combobox({
        panelHeight:-1,
        editable:false,
        onShowPanel:function(){
            $("#policySn").combobox("hidePanel");
            $("#polySnDialog").dialog("open");
        }
    })
    //保单编号对话框
    $("#polySnDialog").dialog({
        title: '选择保险单号',
        left: 200,
        top:100,
        width: 600,
        height: 400,
        closed: true,
        modal: true
    })
    //保单下拉框数据
    $("#polySnDatagrid").datagrid({
        fit: true,
        fitColumns: true,
        striped: true,
        url: '/policy/listForLoss',
        rownumbers: true,
        pagination: true,
        singleSelect: true,
        columns: [[
            {field: 'sn', title: '保险单号', width: 100},
            {field: 'customer', title: '收款人名', width: 100,formatter:customerFormatter}
            ]],
        onDblClickRow:function (index, row) {
            $("#policySn").combobox("setValue",row.sn);
            $("#customer_name").textbox("setValue",row.customer.name);
            $("#polySnDialog").dialog("close");
        }
    });

    //初始员工列表界面
   accident_datagrid.datagrid({
        fit: true,
        fitColumns: true,
        striped: true,
        url: '/accident/list',
        rownumbers: true,
        pagination: true,
        singleSelect: true,
        columns: [[
            {field: 'id', hidden: true},
            {field: 'addr', title: '事故地点', width: 100,formatter: addressFormatter},
             {field: 'tel', title: '报案人电话', width: 100,formatter: telFormatter},
            {field: 'carNo', title: '车牌号', width: 100,formatter: carNumberFormatter},
            {field: 'sn', title: '保单编号', width: 100,formatter: policySnFormatter},
            {field: 'state', title: '状态', width: 100,formatter:statusFormatter},
            {field: 'employee', title: '处理员工', width: 100, formatter: employeeFormatter},
            {field: 'casesDescription', title: '事故描述', width: 100},
            {field: 'responsibility', title: '责任认定', width: 100},
            {field: 'loss', title: '赔损单', width: 100,formatter:lossFormatter},
            {field: 'remark', title: '备注', width: 100},

        ]],
        toolbar: "#tb",
       //事故描述按钮内容切换
        onClickRow:function(index,row){
            if (row.state==1||row.state==4){
                $("#btn_edit").linkbutton({
                    text:"查看描述"
                });
            }else{
                $("#btn_edit").linkbutton({
                    text:"填写描述"
                })
            }
        },
       onLoadSuccess:function(data){
           $("a[name='opera']").linkbutton({plain:true,iconCls:'icon-search'});
       }
    })
        //将方法绑定到对象中
        var cmdObj = {
            //受理事故
            handlerAccident:function(){
                //选中一条需要编辑的数据
                var row = accident_datagrid.datagrid("getSelected");
                if (!row) {
                    $.messager.alert("温馨提示", "亲,请选择一条需要受理的清单", "error");
                    return;
                }
                if (row.state==1){
                    $.messager.alert("温馨提示", "亲,此条数据已受理", "error");
                    return;
                }
                if (row.state==4){
                    $.messager.alert("温馨提示", "亲,此条数据已丢弃", "error");
                    return;
                }
                if (row.state==5){
                    $.messager.alert("温馨提示", "亲,此条数据需处理", "error");
                    return;
                }
                $.messager.confirm('温馨提示', '您想要受理该事故吗？', function(r){
                    if (r){
                        $.post("accident/handlerAccident",{id:row.id,stateId:1},function (data) {
                            if (data.success){
                                $.messager.alert('温馨提示','受理成功','info',function () {
                                    cmdObj.reloadAccident();
                            })}else{
                                $.messager.alert('温馨提示',data.msg,'error')
                            }
                        },"json");
                    }
                });
            },
            //丢弃
            discardAccident:function () {
                var row = accident_datagrid.datagrid("getSelected");
                if (!row) {
                    $.messager.alert("温馨提示", "亲,请选择一条需要丢弃的清单", "error");
                    return;
                }
                if (row.state==4){
                    $.messager.alert("温馨提示", "亲,此条数据已丢弃", "error");
                    return;
                }
                $.messager.confirm('温馨提示', '您想要丢弃该清单吗？', function(r){
                    if (r){
                        $.post("accident/handlerAccident",{id:row.id,stateId:4},function (data) {
                            if (data.success){
                                $.messager.alert('温馨提示','操作成功','info',function () {
                                    cmdObj.reloadAccident();
                                })}else{
                                $.messager.alert('温馨提示',data.msg,'error')
                            }
                        },"json");
                    }
                });
            },
            editAccident:function () {
                //清空form表单中的数据
                accident_form.form("clear");
                $("#accident_form_view").form("clear");
                //选中一条需要编辑的数据
                var row = accident_datagrid.datagrid("getSelected");
                if (!row) {
                    $.messager.alert("温馨提示", "亲,请选择一条数据", "error");
                    return;
                }
                if(row.state==1||row.state==4){
                    //弹出对话框
                    //设置title表单名
                    $("#accident_dialog_view").dialog("setTitle", "事故描述");
                    //弹出对话框
                    $("#accident_dialog_view").dialog("open");
                    $("#accident_form_view").form("load", row);
                    $("#accident_form_view :input").prop("disabled",true);

                }else{
                    //弹出对话框
                    //设置title表单名
                    accident_dialog.dialog("setTitle", "事故描述");
                    //弹出对话框
                    accident_dialog.dialog("open");
                    accident_form.form("load", row);
                }

            },

            reloadAccident:function () {
                accident_datagrid.datagrid("reload");
            },
            saveAccident:function () {
                var row = accident_datagrid.datagrid("getSelected");
                accident_form.form("submit", {
                    url:"accident/save",
                    onSubmit: function(param){
                        param['cases.id'] = row.cases.id;
                        param['lossId'] = row.lossId;
                    },
                    success: function(data){
                        var data = $.parseJSON(data);
                        if (data.success){
                            $.messager.alert('温馨提示','保存成功,已通知报案部门','info',function () {
                                cmdObj.cancelAccident();
                                cmdObj.reloadAccident();
                            });
                        }else{
                            $.messager.alert('温馨提示',data.msg,'error');
                        }
                    }

                });

            },
            //保存/更新赔损明细单
            saveAccidentLoss:function(){
                var row = $("#accident_datagrid").datagrid("getSelected");
                $("#input_form").form("submit", {
                    url:"accident/saveAccidentLoss",
                    onSubmit: function(param){
                        param.accidentId = row.id;
                    },
                    success: function(data){
                        var data = $.parseJSON(data);
                        if (data.success){
                            $.messager.alert('温馨提示','保存成功','info',function () {
                                cmdObj.cancelAccidentLoss();
                                cmdObj.reloadAccident();
                            });
                        }else{
                            $.messager.alert('温馨提示',data.msg,'error');
                        }
                    }

                });
            },
            //状态及关键字查询
            stateSearch:function () {
                var value=$("#searchBox").val();
                var state=$("#stateSearch").val();
                accident_datagrid.datagrid("load",{keyword:value,state:state});
            },
            cancelAccident:function () {
                accident_dialog.dialog("close");
                $("#accident_dialog_view").dialog("close");

            },
            cancelAccidentLoss:function(){
                $("#input_dialog").dialog("close");
                $("#input_dialog_view").dialog("close");
            },
            //reportAccident上报审批
            reportAccident:function () {
                var row = accident_datagrid.datagrid("getSelected");
                if (!row) {
                    $.messager.alert("温馨提示", "亲,请选择一条数据", "error");
                    return;
                }
                if (row.state==0){
                    $.messager.alert("温馨提示", "亲,此条数据待受理", "error");
                    return;
                }
                if (row.state==4){
                    $.messager.alert("温馨提示", "亲,此条数据已丢弃", "error");
                    return;
                }
                $.messager.confirm('温馨提示', '您确定上报审核该事故吗？', function(r){
                    if (r){
                        $.post("accident/handlerAccident",{id:row.id,stateId:2},function (data) {
                            if (data.success){
                                $.messager.alert('温馨提示','上报成功','info',function () {
                                    cmdObj.reloadAccident();
                                })}else{
                                $.messager.alert('温馨提示',data.msg,'error')
                            }
                        },"json");
                    }
                });
            }

        }
        //使用js来绑定事件  使用了属性过滤器来完成该操作
    $("a[data-cmd]").click(function(){
        var cmd = $(this).data("cmd");
        //调用方法
        cmdObj[cmd]();
    });


    //end
    //初始化对话框编辑
    $("#accident_dialog").dialog({
        width: 350,
        height: 470,
        closed: true,
        buttons: "#bb"
    })
    //初始化对话框查看
    $("#accident_dialog_view").dialog({
        width: 350,
        height: 470,
        closed: true,
        buttons:"#accident_dialog_cancel_view"
    })
    //---------------------------
    $("#input_dialog").dialog({
        title:'赔损单明细',
        width: 350,
        height: 370,
        closed: true,
        buttons: "#input_bar"
    })
    $("#input_dialog_view").dialog({
        title:'赔损单明细',
        width: 350,
        height: 370,
        closed: true,
        buttons:"#cancel_view"
    })


})




//员工格式化操作
function employeeFormatter(value, row, index) {
    return value?value.userName:"";
}
//地址格式化操作
function addressFormatter(value, row, index) {
    return row.cases?row.cases.address:"";
}
//电话格式化操作
function telFormatter(value, row, index) {
    return row.cases?row.cases.tel:"";
}
//车牌格式化操作
function carNumberFormatter(value, row, index) {
    return row.cases?row.cases.carNumber:"";
}


//接通员工格式化
function statusFormatter(value, row, index) {
    if (value==0){
        return '<span style=\"color: red\">待受理</span>';
    }else if(value==1){
        return "已受理";
    }else if(value==4){
        return '<span style=\"color: #00ee00\">已丢弃</span>';
    }
    else {
        return '<span style=\"color: #0000FF\">待处理</span>';
    }
}
//赔损单格式化
function lossFormatter(value, row, index) {
    if (row.state==1||row.state==4){
        return '<a onclick="openInputDialogForView('+index+')">查看</a>';
    }else if((row.state==0||row.state==5)){
        return '<a onclick="openInputDialogForEdit('+index+')">编辑</a>';
    }
}
//收款人格式化
function customerFormatter(value, row, index) {
    return row.customer?row.customer.name:"";
}
//赔损单弹出/查看
function openInputDialogForView(index) {
    $("#input_form_view").form("clear");
    $("#input_dialog_view").dialog({
        closed:false
    });
    $("#input_form_view input").prop("disabled",true);
    $("#accident_datagrid").datagrid("selectRow",index);//根据行索引选中行
    var row = $("#accident_datagrid").datagrid("getSelected");
    $.post("accident/getLossInfo",{accidentId:row.lossId},function (data) {
            $("#input_form_view").form("load",data);
    });
}
//赔损单弹出/编辑
function openInputDialogForEdit(index) {
    $("#input_form").form("clear");
    $("#input_dialog").dialog({
        closed:false
    });
    $("#input_form input").prop("disabled",false);
    $("#accident_datagrid").datagrid("selectRow",index);
    var row = $("#accident_datagrid").datagrid("getSelected");
    $.post("accident/getLossInfo",{accidentId:row.lossId},function (data) {
        $("#input_form").form("load",data);
    });
}

//单独定义每行的点击事件
function searchState(policySn) {
    $.post("cases/searchState",{policySn:policySn},function (data) {
        if (data.success){
            $.messager.alert('查询结果',data.msg,'info');
        }else{
            $.messager.alert('查询结果',data.msg,'error');
        }
    })
}

//保单格式化
function policySnFormatter(value, row, index) {
    return   '<a name="opera" class="easyui-linkbutton" onclick="searchState('+row.cases.policySn+')"></a>'+row.cases.policySn;
}
